import { defineStore } from 'pinia';
import { getRoleConfig } from '@/api/role/role';
import type { RoleState, CommonDataItem, CommonTypeItem, CompDetaileConfigItem } from './types';
import type * as ApiTypes from "@/api/role/role.d"
const useRoleStore = defineStore('role', {
  state: (): RoleState => ({
    moduleConfig: {},
    pageConfig: {},
    componentConfig: {},
    compDetaileConfig: {},
    allData: {},
    allDataConfig: [],
    // firstObj: {},
    // secondObj: {},
    // thirdObj: {},
    // fourObj: {},
    // lastObj: {}
    moduleArr: {},
    routerArr: {},
    pageArr: {},
    compArr: {},
    detailArr: {},
    // allData1: {}
  }),

  getters: {
    roleInfo(state: RoleState): RoleState {
      return { ...state };
    },
  },

  actions: {
    async getConfig() {
      const res = await getRoleConfig()
      this.allDataConfig = this.changeResultData(res.list, 1)
      this.cutReduceData(this.allDataConfig)
    },
    /**
     * 通过数组reduce转换数据的函数
     * @param data 需要改变的原始数据
     * @param parentCode 父级code
     * @returns 改变后的结构数组
     */
    changeResultData(data: ApiTypes.RoleDataItem[], level: number, parentCode?: string): CommonDataItem[] {
        return data.reduce((res: CommonDataItem[], cur) => {
          const { childs, ...newCur } = cur
          const children = this.changeChild(childs, parentCode)
          let tempObj: CommonDataItem = {} as CommonDataItem
          if (level === 1) {
            tempObj = {
              ...newCur, parentCode, label: cur.name, checked: false, indeterminate: false, isVisibile: true, children, level
            }
          }
          if(level < 4 && level > 1) {
            tempObj = {
              ...newCur, parentCode, label: cur.name, checked: false, indeterminate: false, children, level
            }
          }
          if (level >= 4) {
            tempObj = {
              ...newCur, parentCode, label: cur.name, checked: false, children, level
            }
          }
          res.push(tempObj)
          if (cur.childs && cur.childs.length) {
            const tempLevel = level + 1
            res = [...res, ...this.changeResultData(cur.childs, tempLevel, cur.code)]
          }
          return res
        }, [])
    },
    /**
     * 当前子级数组结构转换函数
     * @param data 当前需要改变的子级数组
     * @param parentCode 父级code
     * @returns 改变后的数组
     */
    changeChild(data: ApiTypes.RoleDataItem[], parentCode?: string) {
      return data.map(item => {
        // const { childs, ...newItem } = item
        return {
          id: item.id,
          code: item.code,
          label: item.name,
          checked: false,
          indeterminate: false,
          parentCode
        }
      })
    },
    /**
     * 根据接口数据转换成页面渲染数据 value为数组形式
     * @param data 当前数据集合
     * @param stateKey state的key
     * @param parentCode 父级code
     * @param level 当前层级
     */
    changeData(data: ApiTypes.RoleDataItem[], stateKey: string, parentCode?: string, level?: number) {
      data.map(item => {
        if (!parentCode) {
          let list = this[stateKey][item.code]
          if (!list) {
            // list = []
            // this[stateKey][item.code] = list
            list = {
              id: item.id,
              code: item.code,
              label: item.name,
              checked: false,
              indeterminate: false,
              isVisibile: true,
              parentCode,
              level: 1
            }
            this[stateKey][item.code] = list
          }
        } else {
          let list = this[stateKey][parentCode]
          if (!list) {
            list = []
            this[stateKey][parentCode] = list
          }
          list.push({
            id: item.id,
            code: item.code,
            label: item.name,
            checked: false,
            indeterminate: false,
            parentCode,
            level
          })
        }

        if (item.childs && item.childs.length !== 0) {
          // 函数自调用处理深层数据
          !level && this.changeData(item.childs, "routerArr", item.code, 2)
          level === 2 && this.changeData(item.childs, "pageArr", item.code, 3)
          level === 3 && this.changeData(item.childs, "compArr", item.code, 4)
          level === 4 && this.changeData(item.childs, "detailArr", item.code, 5)
        }
      })
    },
    /**
     * 根据接口数据转换成页面渲染数据 value为对象形式
     * @param data 当前数据集合
     * @param key state的key
     * @param parentCode 父级code
     * @param level 当前层级
     */
    changeConfigData(data: ApiTypes.RoleDataItem[], key: string, parentCode?: string, level?: number) {
      data.map(item => {
        // 获取当前state集合中是否存在当前项的code值
        let list = this[key][item.code]
        if (!list) {
          if (!level) {
            // 初始层级不存在
            list = {
              id: item.id,
              code: item.code,
              label: item.name,
              checked: false,
              indeterminate: false,
              isVisibile: true,
              parentCode,
              level: 1
            } as CommonTypeItem
            this[key][item.code] = list
          } else {
            // 后续层级
            list = {
              id: item.id,
              code: item.code,
              label: item.name,
              checked: false,
              indeterminate: false,
              parentCode,
              level
            } as CommonTypeItem
            this[key][item.code] = list
          }
        }
        if (item.childs && item.childs.length !== 0) {
          // 函数自调用处理深层数据
          !level && this.changeConfigData(item.childs, "pageConfig", item.code, 2)
          level === 2 && this.changeConfigData(item.childs, "componentConfig", item.code, 3)
          if (level === 3) {
            // 第三层数据处理层数组
            item.childs.map(t => {
              let temp = this.compDetaileConfig[t.code]
              if (!temp) {
                temp = {
                  id: t.id,
                  code: t.code,
                  label: t.name,
                  checked: false,
                  parentCode: item.code,
                  level: 4,
                  children: []
                } as CompDetaileConfigItem
                // this.allChildConfig[t.code] = {
                //   id: t.id,
                //   code: t.code,
                //   label: t.name,
                //   checked: false,
                //   parentCode: item.code,
                //   level: 4,
                // }
                const tempChild = t.childs && t.childs.map(it => {
                  // this.allChildConfig[it.code] = {
                  //   id: it.id,
                  //   code: it.code,
                  //   label: it.name,
                  //   checked: false,
                  //   level: 5,
                  //   parentCode: temp.code
                  // }
                  return {
                    id: it.id,
                    code: it.code,
                    label: it.name,
                    checked: false,
                    level: 5,
                    parentCode: temp.code
                  }
                })
                temp.children = tempChild
                this.compDetaileConfig[t.code] = temp
              }
            })
          }
        }
      })
      // console.log(this.$state, "处理完事");
    },
    /**
     * 将通过reduce处理的数据转换成viewData函数
     * @param data 需要切割的数据集合
     */
    cutReduceData(data: CommonDataItem[]) {
      this.moduleArr = {}
      this.routerArr = {}
      this.pageArr = {}
      this.compArr = {}
      this.detailArr = {}
      data.map(item => {
        switch (true) {
          case item.level === 1:
            this.moduleArr[item.code] = item
            break;
          case item.level === 2:
            this.cutCommonItem("routerArr", item)
            break;
          case item.level === 3:
            this.cutCommonItem("pageArr", item)
            break;
          case item.level === 4:
            this.cutCommonItem("compArr", item)
            break;
          case item.level === 5:
            this.cutCommonItem("detailArr", item)
            break;
          default:
            break;
        }
      })
    },
    /**
     * 切割转换的通用函数
     * @param stateKey state的key
     * @param item 当前要处理的项的数据
     */
    cutCommonItem(stateKey: string, item: CommonDataItem) {
      let list = this[stateKey][item.parentCode!]
      if (!list) {
        list = []
        this[stateKey][item.parentCode!] = list
      }
      list.push(item)
    }
  }
})

export default useRoleStore;